package weibo4android.androidexamples;

import java.util.ArrayList;
import java.util.HashMap;

import cn.com.karl.logic.AsyncBitmapLoader;
import cn.com.karl.logic.MainService;
import cn.com.karl.logic.Task;
import cn.com.karl.net.NetUtil;

import weibo4android.User;
import weibo4android.androidexamples.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;

/*
 * android:numColumns="auto_fit" ，GridView的列数设置为自动

 android:columnWidth="90dp"，每列的宽度，也就是Item的宽度
 android:stretchMode="columnWidth"，缩放与列宽大小同步
 android:verticalSpacing="10dp"，两行之间的边距，如：行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为 10dp
 android:horizontalSpacing="10dp"，两列之间的边距。*/

public class MsgActivity extends Activity implements IWeiboActivity {
	private GridView gridView;
	private ProgressDialog p;
	private User user;
	private Button head_new;
	private Button head_refresh;
	private TextView head_userName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.msg);

		MainService.addActivity(this);
		MainService.newTask(new Task(Task.MSGACTIVITY, null));

		p = ProgressDialog.show(this, "", "正在加载，请稍候  . . .  ");
		gridView = (GridView) this.findViewById(R.id.gridview);
        
		head_new=(Button) this.findViewById(R.id.head_new);
		head_new.setVisibility(View.GONE);
		
		head_refresh=(Button) this.findViewById(R.id.head_refresh);
		head_refresh.setVisibility(View.GONE);
		
		head_userName=(TextView) this.findViewById(R.id.head_userName);
		head_userName.setText("信息");
	}

	private class ImageList extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 9;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.msg_item, null);
				ImageView imageIcon = (ImageView) convertView
						.findViewById(R.id.msg_item_image);
				TextView text = (TextView) convertView
						.findViewById(R.id.msg_item_textImage);
				//异步加载图片
				new AsyncBitmapLoader().execute(imageIcon,user
						.getProfileImageURL().toString());
				/*imageIcon.setImageDrawable(NetUtil.getNetImage(user
						.getProfileImageURL()));*/
				text.setText(user.getName());
				return convertView;
			}
			return convertView;
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		MainService.removeActivity(this);
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("你确定退出吗？")
					.setCancelable(false)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									MsgActivity.this.finish();
									// 停止服务
									  Intent it=new Intent(MsgActivity.this,MainService.class);
									  MsgActivity.this.stopService(it);
									android.os.Process
											.killProcess(android.os.Process
													.myPid());
									  android.os.Process.killProcess(android.os.Process.myTid());
						              android.os.Process.killProcess(android.os.Process.myUid());
								}
							})
					.setNegativeButton("返回",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh(Object... params) {
		// TODO Auto-generated method stub
		user = (User) params[0];

		gridView.setAdapter(new ImageList());
       
		p.dismiss();
	}

}
